import { Component, Inject } from '@angular/core';
import { IonicPage , NavController,ModalController } from 'ionic-angular';
import { Subscription } from 'rxjs/Subscription';

// service
// 常量
import * as PageConstant from '../../../../constant/pageConstant';
// picker
import { convertEnumToColumn } from 'ion-multi-picker';
// 枚举
import { WorkSeniorityEnum } from '../../../../config/enum';
import { spliceAddress } from '../../../../utils/basic';
/**
 * 工作信息
 */
@IonicPage()
@Component({
  selector: 'page-mine-more',
  templateUrl: 'mine-work.html'
})
export class MineWorkPage{

  // 工作信息
  private workInfo: any;
  // 地址信息
  private address: string;
  private detailAddress: string;

  // 工作年限
  private workSeniority: Array<any>;
  private initWork: any = '';

  // 监听 用户信息
  private userSubscription: Subscription;
  constructor(
    private navCtrl: NavController,
    public modalCtrl: ModalController,
    @Inject('user') private userService,
  )
  {
    this.userSubscription= this.userService.getUser()
      .filter(user=> user.userId)
      .map(user=> user.workInfo)
      .subscribe(workInfo => {
        debugger;
        this.workInfo= workInfo;
        // 解析地址
        const { address, detailAddress }= spliceAddress(workInfo.companyAddr);
        this.address= address;
        this.detailAddress= detailAddress;

        const workingYears = workInfo.workingYears ?
          WorkSeniorityEnum[Number(workInfo.workingYears)]
          :
          '一年至三年';
        this.initPicker(workingYears);
      })
  }
  /*** 页面离开
   * */
  ionViewWillLeave()
  {
    debugger;
    if(typeof this.userSubscription !== 'undefined')
      this.userSubscription.unsubscribe();
  }

  /*** 初始化picker
   * @param education 初始化值
   * */
  initPicker(workingYears)
  {
    this.initWork = WorkSeniorityEnum[workingYears];
    this.workSeniority = convertEnumToColumn(WorkSeniorityEnum);
  }

  /*** 选择地址
   * */
  protected choiceAddress(): void
  {
    let modal = this.modalCtrl.create(PageConstant.LoadMapPage, {},{
      showBackdrop: false,
      enableBackdropDismiss: false,
    });
    modal.onDidDismiss(address => {
      if(!address) return;
      this.address= address;
    });
    modal.present();
  }

  /*** 选择图片
  * */
  executeSelectImage()
  {
    const { workingImg }= this.workInfo;
    let modal = this.modalCtrl.create(PageConstant.WorkPhotoPage, {
      workingImg
    },{
      showBackdrop: false,
      enableBackdropDismiss: false,
    });
    modal.present();
  }

  // 执行保存
  executeSave(): void
  {
    let workingYears= this.workSeniority[0].options[this.initWork].text;
    workingYears= WorkSeniorityEnum[workingYears];
    const companyAddr= `${this.address}#${this.detailAddress}`;
    debugger;
    this.userService.updateWorkInfo({...this.workInfo, companyAddr, workingYears},this.navCtrl);
  }
}

